<template>
  <div class="app-container">
    <!-- 添加或修改余额充值对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body v-drag-fullscreen>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row :gutter="20">
                <el-col :span="8" :xs="24">
                <el-form-item label="房屋" prop="roomNames">
                  <el-input v-model="form.roomNames" class="input-with-select" readonly >
                    <el-button slot="append" icon="el-icon-search" @click="() => this.$refs.rometerRef.show()"></el-button>
                  </el-input>
                </el-form-item>
            </el-col>
                <el-col :span="8" :xs="24">
                <el-form-item label="收费项目" prop="chargeId">
                  <el-select v-model="form.chargeId" placeholder="请输入收费项目" :disabled="disabled">
                    <el-option
                      v-for="item in itemOptions"
                      :key="item.chargeId"
                      :label="item.chargeName"
                      :value="item.chargeId"
                    ></el-option>
                  </el-select>
                </el-form-item>
            </el-col>
                <el-col :span="8" :xs="24">
                <el-form-item label="充值金额" prop="rechargeMoney">
                  <el-input-number  v-model="form.rechargeMoney" :precision="2" :step="0.1" ></el-input-number>
                </el-form-item>
            </el-col>
                <el-col :span="8" :xs="24">
                <el-form-item label="减免金额" prop="derateMoney">
                  <el-input-number  v-model="form.derateMoney" :precision="2" :step="0.1" ></el-input-number>
                </el-form-item>
            </el-col>
                <el-col :span="8" :xs="24">
                <el-form-item label="收款方式" prop="payeeType">
                  <el-select v-model="form.payeeType" placeholder="请选择收款方式" :disabled="disabled">
                    <el-option
                      v-for="dict in dict.collection_method"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
                    ></el-option>
                  </el-select>
                </el-form-item>
            </el-col>
                    <el-col :span="8" :xs="24">
                <el-form-item label="收款人" prop="payeePerson">
                    <el-input v-model="form.payeePerson" placeholder="请输入收款人" :disabled="disabled"/>
                </el-form-item>
            </el-col>
                <el-col :span="8" :xs="24">
                <el-form-item label="收款时间" prop="payeeTime">
                  <el-date-picker clearable size="small" :disabled="disabled"
                    v-model="form.payeeTime"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="选择收款时间">
                  </el-date-picker>
                </el-form-item>
            </el-col>
            <el-col :span="8" :xs="24">
              <el-form-item label="状态" prop="status">
                <el-select v-model="form.status" placeholder="请选择状态" :disabled="disabled">
                  <el-option
                    v-for="item in dict.charge_status "
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
                <el-col :span="8" :xs="24">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="disabled"/>
                </el-form-item>
            </el-col>
              </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <TableSelect
      ref="rometerRef"
      :listColumns="rometerColumns"
      gridUrl="/property/room/list"
      @rowClick="rometerSelect"
      title="仪表字典"
    />
  </div>
</template>

<script>
import { getBalanceRefill , addBalanceRefill, updateBalanceRefill } from "@/api/payfee/balanceRefill";
import {listItem} from "@/api/payfee/item";
import  {listRoom,getRoom} from "@/api/property/room"
export default {
  name: "BalanceRefillFrom",
  dicts: ['collection_method',  'charge_status',],
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否禁用
      disabled: false,
      // 表单参数
      form: {},
      itemOptions:[],
      // 表单校验
      rules: {
        chargeId:[
          { required: true, message: "收费项目不能为空", trigger: "blur" }
        ],
        roomNames: [
          { required: true, message: "房屋不能为空", trigger: "blur" }
        ],
        rechargeMoney: [
          { required: true, message: "充值金额不能为空", trigger: "blur" }
        ],
      },
      rometerColumns: [
        {title: '业主姓名', key: 'customName', type: '', width: '', isSearch: true},
        {title: '楼栋', key: 'floorName', type: '', width: '', isSearch: true},
        {title: '房间', key: 'roomName', type: '', width: '', isSearch: false},
      ],
    };
  },
  created() {
    listItem({projectIdIN:"'01','02','05'"}).then(res=>{
      this.itemOptions=res.rows;
    })
  },
  methods: {
    show(option){
      console.log("option.node",option.node)
      this.reset();
      this.disabled = option.type == 'VIEW'
      if(option.type=='ADD'){
          this.open = true;
          this.title = "新增余额充值";
        //检查左边树节点是否是房屋
        this.getRoomMeterInfo(option.node);
        this.form.payeePerson = this.$store.state.user.nickName;
        this.form.payeeTime = this.parseTime(new Date());
      }else if(option.type == 'UPDATE' || option.type == 'VIEW'){
          getBalanceRefill(option.id).then(response => {
              this.form = response.data;
            this.form.roomNames =`${this.form.roomName}[${this.form.customName}]`
              if(option.type == 'VIEW'){
                  this.title = "查看余额充值";
              }else{
                  this.title = "修改余额充值";
              }
              this.open = true;
          });
      }
    },
    getRoomMeterInfo(currentNode){
      console.log("currentNode",currentNode)
      if (currentNode && currentNode.modelType == 'room') {
        getRoom(currentNode.id).then(res => {
            let row = res.data
            this.form.roomNames = `${row.roomName}[${row.customName}]`
            this.form.roomId = row.id
            this.form.customId = row.ownerId
            this.form.communityId = row.communityId
        })
      }
    console.log("this.form",this.form)
    },
    rometerSelect(index, row) {
      this.form.roomNames =`${row.roomName}[${row.customName}]`
      this.form.roomId = row.id
      this.form.customId = row.ownerId
      this.form.communityId = row.communityId
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("cancel")
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        balanceOdd: null,
        communityId: null,
        roomId: null,
        customId: null,
        chargeId: null,
        rechargeMoney: null,
        derateMoney: null,
        payeeType: null,
        payeePerson: null,
        payeeTime: null,
        deptId: null,
        ancestors: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        roomNames:'',
        status: '10001',
        rechargeMoney: null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      console.log("房间ID",this.form.roomId)
      //return
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBalanceRefill(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.cancel()
            });
          } else {
            addBalanceRefill(this.form).then(response => {
              this.msgSuccess("新增成功");
                this.cancel()
            });
          }
        }
      });
    }
  }
};
</script>
<style scoped>
  .el-dialog__body{
    padding: 10px 20px;
  }
</style>
